package trading.formulas.functions;

import java.util.Calendar;

import trading.Parameters;
import trading.data.Controller;
import trading.data.IDataType;
import trading.data.IStockTickData;
import trading.data.IStockTickDataCollection;


public class Ref implements IFunction{

	/**
	 * parameters : cycle 
	 */
	private Parameters PARAM = new Parameters(new int[][]{{100,1,5,1}});
	
	final public static int CYCLE_IDX = 0;
	
	public static Ref getRef(){return new Ref();}
	
	private Ref(){}
	
	@Override
	public Parameters getParameters() {
		return PARAM;
	}
	
	@Override
	public String getName() {
		return "REF";
	}

	@Override
	public float getValue(String dataType, IStockTickDataCollection dataCollection, int index) throws TradingFunctionException 
	{
		return getValue(dataType, dataCollection, index, BasicFunctionProvider.INSTANCE);
	}
	
	@Override
	public float getValue(String dataType,
			IStockTickDataCollection dataCollection, int index,
			IDataFunctionProvider provider) {
		
		int cycle = getParameters().getParamValue(CYCLE_IDX);
		if(index < cycle)
		{
			throw new TradingFunctionException("refered date out of boundary");
		}
		
		return provider.getValue(dataType, dataCollection, index - cycle);
	}
	
	public static void main(String[] args) {
		Calendar cal = Controller.createZeroCaledar();
		cal.set(2006, 05 - 1, 15);
		IStockTickDataCollection dataCollection = Controller.getTickData();
		int index = Controller.getIndex(dataCollection, cal, true);
		System.out.println(index);
//		index = 10;
		Ref ref = Ref.getRef();
		Parameters params = ref.getParameters();
		params.setParamValue(CYCLE_IDX, 5);
		System.out.println(ref.getValue(IDataType.CLOSE_NAME, dataCollection, index));
	}

}
